package com.ruoyi.system.service.impl.cooperation;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.db.cooperation.ExternalCooperation;
import com.ruoyi.system.domain.dto.ExternalCooperationDto;
import com.ruoyi.system.domain.dto.TradeCargoOrderDto;
import com.ruoyi.system.domain.vo.ExternalCooperationVo;
import com.ruoyi.system.mapper.cooperation.ExternalCooperationMapper;
import com.ruoyi.system.service.IExternalCooperationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ExternalCooperationServiceImpl implements IExternalCooperationService {

    @Resource
    private ExternalCooperationMapper externalCooperationMapper;

    @Override
    public ExternalCooperationVo selectExternalCooperationOrderByOrderId(Long orderId) {
        ExternalCooperationVo externalCooperationVo = new ExternalCooperationVo();
        if (orderId != null) {
            ExternalCooperationDto externalCooperationDto = externalCooperationMapper.selectExternalCooperationOrderByOrderId(orderId);
            externalCooperationVo = dtoToVo(externalCooperationDto);
        }
        return externalCooperationVo;
    }

    @Override
    public List<ExternalCooperationVo> selectExternalCooperationOrderList(ExternalCooperation externalCooperation) {
        List<ExternalCooperationDto> externalCooperationDtos = externalCooperationMapper.selectExternalCooperationOrderList(externalCooperation);
        return externalCooperationDtos.stream().map(this::dtoToVo).collect(Collectors.toList());
    }

    @Override
    public int insertExternalCooperationOrder(ExternalCooperation externalCooperation) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Date nowDate = DateUtils.getNowDate();
        externalCooperation.setCreateTime(nowDate);
        externalCooperation.setCreateBy(loginUser.getUser().getUserId());
        return externalCooperationMapper.insertExternalCooperationOrder(externalCooperation);
    }

    @Override
    public int updateExternalCooperationOrder(ExternalCooperation externalCooperation) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Date nowDate = DateUtils.getNowDate();
        externalCooperation.setUpdateTime(nowDate);
        externalCooperation.setUpdateBy(loginUser.getUser().getUserId());
        return externalCooperationMapper.updateExternalCooperationOrder(externalCooperation);
    }

    @Override
    public int deleteExternalCooperationOrderByOrderIds(List<Long> orderIds) {
        return externalCooperationMapper.deleteExternalCooperationOrderByOrderIds(orderIds);
    }

    @Override
    public int deleteExternalCooperationOrderByOrderId(Long orderId) {
        return externalCooperationMapper.deleteExternalCooperationOrderByOrderId(orderId);
    }

    private ExternalCooperationVo dtoToVo(ExternalCooperationDto dto){
        ExternalCooperationVo vo = new ExternalCooperationVo();
        vo.setAdvertiseId(dto.getAdvertiseId());
        vo.setBasicCompanies(dto.getBasicCompanies());
        vo.setSalary(dto.getSalary());
        vo.setDescription(dto.getDescription());
        vo.setPictureUrl(dto.getPictureUrl());
        vo.setCreateBy(dto.getCreateBy());
        vo.setCreateTime(dto.getCreateTime());
        vo.setUpdateBy(dto.getUpdateBy());
        vo.setUpdateTime(dto.getUpdateTime());
        return vo;
    }
}
